﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Nova_Common;

public partial class Views_ABCKind : System.Web.UI.Page
{
    DataSet ds = null;
    Nova_BLL.TBase_Position Nova_BLL_TBase_Position = new Nova_BLL.TBase_Position();
    Nova_BLL.TLaneDefine Nova_BLL_TLaneDefine = new Nova_BLL.TLaneDefine();
    Nova_BLL.ABC_Class Nova_BLL_ABC_Class = new Nova_BLL.ABC_Class();
    int iPos = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();

        Bind_BC_ABC();
        if (!IsPostBack)
        {
            ddlNo_Lane_ABC_Bind();
            Bind_ABC();
            Bind_Style();
        }
        gv_ABC_Bind();
    }


    /// <summary>
    /// 巷道下拉列表绑定
    /// </summary>
    protected void ddlNo_Lane_ABC_Bind()
    {
        ds = Nova_BLL_TLaneDefine.DataSetTLaneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane_ABC.DataSource = ds;
            ddlNo_Lane_ABC.DataBind();
        }
    }

    /// <summary>
    /// 巷道改变时gv发生改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlNo_Lane_ABC_SelectedIndexChanged(object sender, EventArgs e)
    {
        gv_ABC_Bind();
        Bind_ABC();
    }
    protected void Bind_BC_ABC()
    {
        //ABC背景色
        labBC_A_ABC.BackColor = System.Drawing.Color.FromName(Nova_BLL_ABC_Class.BackColorSysoption("A"));
        labBC_B_ABC.BackColor = System.Drawing.Color.FromName(Nova_BLL_ABC_Class.BackColorSysoption("B"));
        labBC_C_ABC.BackColor = System.Drawing.Color.FromName(Nova_BLL_ABC_Class.BackColorSysoption("C"));
    }


    /// <summary>
    /// 样式
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Bind_ABC()
    {
        iPos = Nova_BLL_ABC_Class.CountPosID(ddlNo_Lane_ABC.SelectedValue);
        labNum_A_ABC.Text = Nova_BLL_ABC_Class.CountSysoption(ddlNo_Lane_ABC.SelectedValue, "1").ToString();
        labNum_B_ABC.Text = Nova_BLL_ABC_Class.CountSysoption(ddlNo_Lane_ABC.SelectedValue, "2").ToString();
        labNum_C_ABC.Text = Nova_BLL_ABC_Class.CountSysoption(ddlNo_Lane_ABC.SelectedValue, "3").ToString();

        if (labNum_A_ABC.Text == "0")
        {
            txtPercentage_A_ABC.Text = "0";
        }
        else
        {
            txtPercentage_A_ABC.Text = Convert.ToString(double.Parse(labNum_A_ABC.Text) / iPos * 100);
        }

        if (labNum_B_ABC.Text == "0")
        {
            txtPercentage_B_ABC.Text = "0";
        }
        else
        {
            txtPercentage_B_ABC.Text = Convert.ToString(double.Parse(labNum_B_ABC.Text) / iPos * 100);
        }

        if (labNum_C_ABC.Text == "0")
        {
            txtPercentage_C_ABC.Text = "0";
        }
        else
        {
            txtPercentage_C_ABC.Text = Convert.ToString(double.Parse(labNum_C_ABC.Text) / iPos * 100);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void txtPercentage_A_ABC_TextChanged(object sender, EventArgs e)
    {
        decimal dPercentage = iPos * Convert.ToDecimal(txtPercentage_A_ABC.Text) / 100;
        labNum_A_ABC.Text = dPercentage.ToString();
    }
    protected void txtPercentage_B_ABC_TextChanged(object sender, EventArgs e)
    {
        decimal dPercentage = iPos * Convert.ToDecimal(txtPercentage_B_ABC.Text) / 100;
        labNum_B_ABC.Text = dPercentage.ToString();
    }
    protected void txtPercentage_C_ABC_TextChanged(object sender, EventArgs e)
    {
        decimal dPercentage = iPos * Convert.ToDecimal(txtPercentage_C_ABC.Text) / 100;
        labNum_C_ABC.Text = dPercentage.ToString();
    }

    /// <summary>
    /// 修改ABC百分比
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSave_ABC_Click(object sender, ImageClickEventArgs e)
    {
        float fA = float.Parse(txtPercentage_A_ABC.Text) / 100;
        float fB = float.Parse(txtPercentage_B_ABC.Text) / 100;
        float fC = float.Parse(txtPercentage_C_ABC.Text) / 100;

        float fcount = fA + fB + fC;
        if (fcount == 1.0)
        {
            string strErr = Nova_BLL_ABC_Class.UpdateSysoption(ddlNo_Lane_ABC.SelectedValue.ToString(), fA, fB, fC);
            labShowMessage.Text = strErr;
        }
        else
        {
            labShowMessage.Text = "您输入的ABC分类比例合计数不等于1，请检查。";
        }
        Bind_ABC();
    }

    /// <summary>
    /// 绑定宽度高度
    /// </summary>
    protected void Bind_Style()
    {
        HttpCookie cookies = Request.Cookies["vNovaUserNo"];
        ds = Nova_BLL_TBase_Position.getGVStyle("TSysoption", cookies.Value);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            txtWidth_ABC.Text = ds.Tables[0].Rows[0]["Width"].ToString();
            txtHeight_ABC.Text = ds.Tables[0].Rows[0]["Height"].ToString();
            gv_ABC.Width = Unit.Parse(ds.Tables[0].Rows[0]["Width"].ToString());
            gv_ABC.Height = Unit.Parse(ds.Tables[0].Rows[0]["Height"].ToString());
        }
    }

    /// <summary>
    /// 样式取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnCancel_ABC_Click(object sender, ImageClickEventArgs e)
    {
        Bind_Style();
    }

    /// <summary>
    /// 样式确定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnOk_ABC_Click(object sender, ImageClickEventArgs e)
    {
        HttpCookie cookies = Request.Cookies["vNovaUserNo"];
        int iFlag = Nova_BLL_TBase_Position.iBackGVStyle(txtWidth_ABC.Text.ToString(), txtHeight_ABC.Text.ToString(), "TSysoption", cookies.Value);
        if (iFlag > 0)
        {
            gv_ABC.Width = Unit.Parse(txtWidth_ABC.Text.ToString());
            gv_ABC.Height = Unit.Parse(txtHeight_ABC.Text.ToString());
        }
        else
        {
            ShowBox.MessageBox(this.Page, "修改失败！！！");
        }
    }

    /// <summary>
    /// 绑定gv
    /// </summary>
    protected void gv_ABC_Bind()
    {
        ds = Nova_BLL_ABC_Class.ListSysoption(ddlNo_Lane_ABC.SelectedValue.ToString());
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            gv_ABC.DataSource = ds;
            gv_ABC.DataBind();
        }
    }

    /// <summary>
    /// gv背景色
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_ABC_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells.Count >= 2)
            {
                //e.Row.Cells[2].Text = "?";
                //for (int i = 3; i < e.Row.Cells.Count; i++)
                for (int i = 2; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Text == "1")
                    {
                        e.Row.Cells[i].BackColor = System.Drawing.Color.FromName(Nova_BLL_ABC_Class.BackColorSysoption("A"));//改变背景色
                    }
                    else if (e.Row.Cells[i].Text == "2")
                    {
                        e.Row.Cells[i].BackColor = System.Drawing.Color.FromName(Nova_BLL_ABC_Class.BackColorSysoption("B"));//改变背景色
                    }
                    else
                    {
                        e.Row.Cells[i].BackColor = System.Drawing.Color.FromName(Nova_BLL_ABC_Class.BackColorSysoption("C"));//改变背景色
                    }
                    e.Row.Cells[i].Text = "";
                }
            }
        }
    }

}
